﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Animation;

namespace AgLux
{
    /// <summary>
    /// //
    /// </summary>

    public class Component : Control
    {
        public event EventHandler StandardSize;
        public event EventHandler FullSize;

        protected FrameworkElement _resource;

        protected Canvas _screen;
        protected Canvas _control;
        protected Canvas _sizeStandard, _sizeFull;
        protected TextBlock _caption;

        protected Storyboard _showControl, _timeControl, _hideControl;

        protected State _controlState;
        protected bool _isFocusingControl;
        protected double _proportion, _filling;

        /// <summary>
        /// //
        /// </summary>

        protected Component()
            : base()
        {
            _resource = InitializeFromXaml(new StreamReader(GetType().Assembly.GetManifestResourceStream("AgLux.Component.xaml")).ReadToEnd());

            _screen = (Canvas)_resource.FindName("screen");
            _control = (Canvas)_resource.FindName("control");
            _sizeStandard = (Canvas)_resource.FindName("sizeStandard");
            _sizeFull = (Canvas)_resource.FindName("sizeFull");
            _caption = (TextBlock)_resource.FindName("caption");

            _showControl = (Storyboard)_resource.FindName("showControl");
            _timeControl = (Storyboard)_resource.FindName("timeControl");
            _hideControl = (Storyboard)_resource.FindName("hideControl");

            _controlState = State.hidden;
            _filling = _proportion = 1;
            _isFocusingControl = false;

            _sizeStandard.MouseLeftButtonDown += new MouseEventHandler(SizeStandard);
            _sizeFull.MouseLeftButtonDown += new MouseEventHandler(SizeFull);

            _showControl.Completed += new EventHandler(TimeControl);
            _timeControl.Completed += new EventHandler(HideControl);

            BrowserHost.FullScreenChange += new EventHandler(BrowserHost_FullScreenChange);
            
// l337 haxxorz beware
        }

        void BrowserHost_FullScreenChange(object sender, EventArgs e)
        {
           MouseEventArgs arg = new MouseEventArgs();
            if (!BrowserHost.IsFullScreen)
            {
               SizeStandard(_sizeStandard, arg);
            }
        }


        /// <summary>
        /// //
        /// </summary>

        /// <param name="canvasWidth">
        /// //
        /// </param>

        public virtual void FitCanvas(double canvasWidth)
        {
            Width = canvasWidth;
            ScreenWidth = canvasWidth * _filling;

            Height = ScreenHeight = ScreenWidth * _proportion;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="value">
        /// //
        /// </param>

        protected virtual double ScreenWidth
        {
            get
            {
                return _screen.Width;
            }

            set
            {
                _screen.Width = value;
            }
        }
        /// <summary>
        /// //
        /// </summary>

        /// <param name="value">
        /// //
        /// </param>

        protected virtual double ScreenHeight
        {
            get
            {
                return _screen.Height;
            }

            set
            {
                _screen.Height = value;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        protected virtual void SetControl()
        {
            _control.SetValue(Canvas.LeftProperty, ScreenWidth / 2 - _control.Width / 2);
            _control.SetValue(Canvas.TopProperty, ScreenHeight / 2 - _control.Height / 2);
        }

        /// <summary>
        /// //
        /// </summary>

        protected virtual void SetCaption()
        {
            if (_caption.Text != "")
            {
                _caption.Width = ScreenWidth;
                _caption.Height = _caption.ActualHeight + 5;
                _caption.SetValue(Canvas.TopProperty, ScreenHeight + 5);
                _caption.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void SizeStandard(object sender, MouseEventArgs args)
        {
            if (BrowserHost.IsFullScreen)
            {
                BrowserHost.IsFullScreen = false;
            }

            ScreenWidth = Width * _filling;
            ScreenHeight = ScreenWidth * _proportion;

            _sizeStandard.Visibility = Visibility.Collapsed;
            _sizeFull.Visibility = Visibility.Visible;

            OnStandardSize(new EventArgs());
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void SizeFull(object sender, MouseEventArgs args)
        {
            BrowserHost.IsFullScreen = true;

            ScreenWidth = BrowserHost.ActualWidth;
            ScreenHeight = BrowserHost.ActualHeight;

            _sizeFull.Visibility = Visibility.Collapsed;
            _sizeStandard.Visibility = Visibility.Visible;

            OnFullSize(new EventArgs());
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void ShowControl(object sender, MouseEventArgs args)
        {
            if (_controlState == State.hidden)
            {
                _timeControl.Stop();
                _hideControl.Stop();
                _showControl.Begin();
                _controlState = State.changing;
            }

            else if (_controlState == State.shown)
            {
                _showControl.Stop();
                _hideControl.Stop();
                _timeControl.Begin();
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void TimeControl(object sender, EventArgs args)
        {
            _showControl.Stop();
            _hideControl.Stop();
            _timeControl.Begin();
            _controlState = State.shown;
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void HideControl(object sender, EventArgs args)
        {
            if (_isFocusingControl)
            {
                _showControl.Stop();
                _hideControl.Stop();
                _timeControl.Begin();
            }

            else
            {
                _showControl.Stop();
                _timeControl.Stop();
                _hideControl.Begin();
                _controlState = State.hidden;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="sender">
        /// //
        /// </param>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void FocusControl(object sender, EventArgs args)
        {
            if (_isFocusingControl)
            {
                _isFocusingControl = false;
            }

            else
            {
                _isFocusingControl = true;
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void OnStandardSize(EventArgs args)
        {
            if (StandardSize != null)
            {
                StandardSize(this, args);
            }
        }

        /// <summary>
        /// //
        /// </summary>

        /// <param name="args">
        /// //
        /// </param>

        protected virtual void OnFullSize(EventArgs args)
        {
            if (FullSize != null)
            {
                FullSize(this, args);
            }
        }
    }
}
